import { asyncRoutes, constantRoutes } from '@/router/index'

export default {
  namespaced: true,
  state: {
    // 提供给侧边栏进行展示
    routes: constantRoutes// 该用户的路由映射 【初始为静态】【后面再加上静态】
  },
  getters: {

  },
  mutations: {
    setRoutes(state, data) {
      state.routes = [...constantRoutes, ...data]
    }
  },
  actions: {
    // 根据后端返回的权限标志，和本地完整的动态路由数组进行筛选
    filterRoutes(context, data) {
      const userAsyncRoutes = []
      asyncRoutes.forEach(item => {
        // 判断item.name
        if (data.includes(item.name)) {
          userAsyncRoutes.push(item)
        }
      })
      //   把*通配的404界面放在动态路由的最后面
      userAsyncRoutes.push({ path: '*', redirect: '/404', hidden: true })
      context.commit('setRoutes', userAsyncRoutes)
      //   console.log(userAsyncRoutes) // 删选出属于当前用户的
      return userAsyncRoutes
    }
  }
}
